package com.hxkcloud.nurse.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.YjhApplication;
import com.hxkcloud.nurse.beans.BetterPlanEditBean;
import com.hxkcloud.nurse.beans.DolistEditFuncBean;
import com.hxkcloud.nurse.utils.ImageUtil;
import com.hxkcloud.nurse.viewholders.NewDolistViewholder;

import java.util.List;

/**
 * Created by william_sun on 2016-11-28.
 * Description:新建康复计划单
 */
public class NewBetterPlanAdapter extends RecyclerView.Adapter<NewDolistViewholder> {

    private Context mContext;
    private List<BetterPlanEditBean> datas;
    private int screenWidth;
    private NewDolistAdapter.NewDolistListener listener;

    public NewBetterPlanAdapter(Context mContext, List<BetterPlanEditBean> datas, int width,
                                NewDolistAdapter.NewDolistListener listener) {
        this.mContext = mContext;
        this.datas = datas;
        this.screenWidth = width;
        this.listener = listener;
    }

    public void setData(List<BetterPlanEditBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }


    @Override
    public NewDolistViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContext).inflate(R.layout.adapter_newdolist, null);
        NewDolistViewholder holder = new NewDolistViewholder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(NewDolistViewholder holder, final int position) {
        if (position == 0) {
            holder.view_line1.setVisibility(View.INVISIBLE);
            holder.view_line2.setVisibility(View.VISIBLE);
            holder.iv_sign.setImageResource(R.drawable.remind);
            holder.tv_add.setVisibility(View.GONE);
//            holder.bt_dayindex.setBackgroundResource(R.drawable.bt_grey_halfcircle);
        } else if (position == getItemCount() - 1) {
            holder.view_line1.setVisibility(View.VISIBLE);
            holder.view_line2.setVisibility(View.INVISIBLE);
            holder.iv_sign.setImageResource(R.drawable.remind_btn_add);
            holder.tv_add.setVisibility(View.VISIBLE);
//            holder.bt_dayindex.setBackgroundResource(R.drawable.bt_blue_halfcircle);
        } else {
            holder.view_line1.setVisibility(View.VISIBLE);
            holder.view_line2.setVisibility(View.VISIBLE);
            holder.iv_sign.setImageResource(R.drawable.remind_btn_detele);
            holder.tv_add.setVisibility(View.GONE);
//            holder.bt_dayindex.setBackgroundResource(R.drawable.bt_blue_halfcircle);
        }

        holder.iv_sign.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onSignClick(position);
            }
        });
        holder.tv_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onSignClick(position);
            }
        });
        if (position < datas.size()) {
            holder.tv_add.setVisibility(View.GONE);
            holder.ll_item.setVisibility(View.VISIBLE);
            holder.bt_dayindex.setVisibility(View.VISIBLE);
            holder.bt_dayindex.setText("已选" + datas.get(position).getDate().size() + "天");
            holder.bt_dayindex.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.addDayIndex(position);
                }
            });

            holder.bt_addnotice.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onAddnotice(position);
                }
            });

            holder.ll_items.removeAllViews();

            if (datas.get(position).getRemindList() != null && datas.get(position).getRemindList().size() > 0) {
                for (int i = 0; i < datas.get(position).getRemindList().size(); i++) {
                    LinearLayout ll_function = new LinearLayout(mContext);
                    LinearLayout.LayoutParams lp_ll = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, ImageUtil.dip2px(50, mContext));
                    ll_function.setLayoutParams(lp_ll);
                    ll_function.setOrientation(LinearLayout.HORIZONTAL);
                    ll_function.setPadding(ImageUtil.dip2px(10, mContext), ImageUtil.dip2px(5, mContext), ImageUtil.dip2px(10, mContext), ImageUtil.dip2px(5, mContext));
                    ll_function.setGravity(Gravity.CENTER_VERTICAL);

                    ImageView iv_del = new ImageView(mContext);
                    LinearLayout.LayoutParams lp_iv = new LinearLayout.LayoutParams(screenWidth / 12, screenWidth / 12);
                    lp_iv.leftMargin = ImageUtil.dip2px(10, mContext);
                    iv_del.setLayoutParams(lp_iv);
                    iv_del.setImageResource(R.drawable.remind_delete);

                    final int child = i;
                    iv_del.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            listener.onDeleteFunction(position, child);
                        }
                    });

                    DolistEditFuncBean.funcBean func = datas.get(position).getRemindList().get(i);
                    if (func.getType().equals(YjhApplication.doorder_type_content)) {
                        EditText et_word = new EditText(mContext);
                        LinearLayout.LayoutParams lp_et = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                        lp_et.weight = 1;
                        et_word.setLayoutParams(lp_et);
                        et_word.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                        et_word.setText(func.getContent());
                        et_word.setTextColor(ContextCompat.getColor(mContext, R.color.grey_text1));
                        et_word.setTextSize(14);
                        et_word.setHint(R.string.clickinputwordnotice);
                        et_word.setFilters(new InputFilter[]{new InputFilter.LengthFilter(30)});
//                        et_word.setOnFocusChangeListener(new View.OnFocusChangeListener() {
//                            @Override
//                            public void onFocusChange(View v, boolean hasFocus) {
//                                if (!hasFocus) {
//                                    String text = ((EditText) v).getText().toString().trim();
//                                    datas.get(position).getItemList().get(child).setContent(text);
//                                }
//                            }
//                        });
                        et_word.addTextChangedListener(new TextWatcher() {
                            @Override
                            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                            }

                            @Override
                            public void onTextChanged(CharSequence s, int start, int before, int count) {
                                String text = s.toString();
                                datas.get(position).getRemindList().get(child).setContent(text);
                            }

                            @Override
                            public void afterTextChanged(Editable s) {

                            }
                        });
                        ll_function.addView(et_word);
                        ll_function.addView(iv_del);
                    } else {
                        TextView tv_monitor = new TextView(mContext);
                        LinearLayout.LayoutParams lp_tv = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                        lp_tv.weight = 1;
                        tv_monitor.setLayoutParams(lp_tv);
                        tv_monitor.setTextColor(ContextCompat.getColor(mContext, R.color.light_blue));
                        tv_monitor.setText(func.getContent());
                        tv_monitor.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
                        tv_monitor.setTextSize(14);
                        ll_function.addView(tv_monitor);
                        ll_function.addView(iv_del);
                    }
                    holder.ll_items.addView(ll_function);
                    if (i != datas.get(position).getRemindList().size() - 1) {
                        View view = new View(mContext);
                        LinearLayout.LayoutParams lp_view = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1);
                        view.setLayoutParams(lp_view);
                        view.setBackgroundColor(ContextCompat.getColor(mContext, R.color.grey_text2));
                        holder.ll_items.addView(view);
                    }
                }
                holder.view_divider.setVisibility(View.VISIBLE);
            } else {
                holder.view_divider.setVisibility(View.GONE);
            }

            holder.ll_times.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onAddTime(position);
                }
            });

            holder.ll_times1.setVisibility(View.VISIBLE);

            holder.ll_times1.removeAllViews();
            holder.ll_times2.removeAllViews();
            for (int i = 0; i < Math.min(4, datas.get(position).getTime().size()); i++) {
                String time = datas.get(position).getTime().get(i);
                TextView tv_time = new TextView(mContext);
                LinearLayout.LayoutParams lp_time = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                lp_time.rightMargin = ImageUtil.dip2px(5, mContext);
                tv_time.setLayoutParams(lp_time);
                tv_time.setTextColor(ContextCompat.getColor(mContext, R.color.grey_text1));
                tv_time.setTextSize(14);
                tv_time.setBackgroundResource(R.drawable.foundation_grey_halfcircle);
                tv_time.setPadding(ImageUtil.dip2px(10, mContext), 0, ImageUtil.dip2px(10, mContext), 0);
                tv_time.setText(time);
                holder.ll_times1.addView(tv_time);
            }

            if (datas.get(position).getTime().size() > 4) {
                holder.ll_times2.setVisibility(View.VISIBLE);
                for (int i = 4; i < datas.get(position).getTime().size(); i++) {
                    String time = datas.get(position).getTime().get(i);
                    TextView tv_time = new TextView(mContext);
                    LinearLayout.LayoutParams lp_time = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    lp_time.rightMargin = ImageUtil.dip2px(5, mContext);
                    tv_time.setLayoutParams(lp_time);
                    tv_time.setTextColor(ContextCompat.getColor(mContext, R.color.grey_text1));
                    tv_time.setTextSize(14);
                    tv_time.setBackgroundResource(R.drawable.foundation_grey_halfcircle);
                    tv_time.setPadding(ImageUtil.dip2px(10, mContext), 0, ImageUtil.dip2px(10, mContext), 0);
                    tv_time.setText(time);
                    holder.ll_times2.addView(tv_time);
                }
            } else {
                holder.ll_times2.setVisibility(View.GONE);
            }
        } else {
            holder.tv_add.setVisibility(View.VISIBLE);
            holder.ll_item.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return datas.size() + 1;
    }

}
